-- region HeroCellSmall.lua
-- Author : kevinz
-- Date   : 2015/03/28
-- Description:英雄框通用控件

require "tagMap/Tag_hero_cell_small"

HeroCellSmall = class("HeroCellSmall",function()
    local ret = cc.Node:create()

    -- 组件定义区
    -- 英雄模型ID
    ret.nHeroID = nil
    -- 站位按钮 可以注册点击事件
    ret.btn_place = nil
    -- 英雄头像图标
    ret.img_hero_portrait = nil
    -- 品质框
    ret.img_quality = nil
    -- 星级图标
    ret.img_star = nil
    -- 培养提示图标
    ret.img_up_1 = nil
    -- 英雄等级背景 根据品质显示不同颜色的背景
    ret.img_hero_level_bg = nil
    -- 英雄等级展示lab
    ret.lab_hero_level = nil
    -- 英雄灵魂石数量进度条
    ret.prog_hero_soul_stone = nil
    -- 英雄灵魂石数量显示label
    ret.lab_hero_soul_stone_prog = nil
    -- 图标表面透明黑色遮罩(默认隐藏 在英雄主界面没有获得该英雄时 显示该遮罩)
    ret.img_transparent_mask = nil
    -- 点击事件回调
    ret.onClickHeroItem = nil
    -- 英雄出战图标
--    ret.img_go_into_battle_tag = nil
    -- 英雄怒气条
    ret.prog_hero_anger = nil
    -- 英雄血条
    ret.prog_hero_hp = nil
    -- 英雄星级
    ret.img_star_bg = nil
    -- 死亡背景图
    ret.img_die_bg = nil
    -- 死亡文字
    ret.lab_die_txt = nil
    -- 背景
    ret.img_hero_quality_0 = nil
    return ret
end)

HeroCellSmall.__index = HeroCellSmall

-- 提示箭头移动速度(s)
local UP_DOWN_SPEED = 0.3
-- 提示箭头幅度(像素)
local UP_DOWN_RANGE = 5

-- 创建并且替换现有的node
function HeroCellSmall:createAndReplaceNode(node)
    local ret = HeroCellSmall:create()
    replaceNode(ret,node)
    return ret
end

function HeroCellSmall:create()
    local ret = HeroCellSmall:new()
    ret:init()
    return ret
end

function HeroCellSmall:init()
    TuiManager:getInstance():parseScene(self,"panel_small_hero_cell",PATH_HERO_CELL_SMALL)
    local panel_main = findChildByTag(self,Tag_hero_cell_small.PANEL_SMALL_HERO_CELL1)

    self.btn_place = findChildByTag(self,Tag_hero_cell_small.BTN_PLACE)
    self.img_hero_portrait = findChildByTag(self,Tag_hero_cell_small.IMG_HERO_PLACE_80)
    --self.img_hero_portrait:setScale(0.5)
    
    self.img_quality = findChildByTag(self,Tag_hero_cell_small.IMG_HERO_QUALITY_1)
    self.img_star = findChildByTag(self,Tag_hero_cell_small.IMG_YELLOW_STAR_1)
    --self.img_hero_level_bg = findChildByTag(self,Tag_hero_cell_small.IMG_HERO_LEVEL_BG_1)
    self.img_hero_level_bg = findChildByTag(self,Tag_hero_cell_small.IMG_HERO_LEVEL_BG)
    self.lab_hero_level = findChildByTag(self,Tag_hero_cell_small.LAB_HERO_LEVEL)
    self.img_hero_quality_0 = findChildByTag(self,Tag_hero_cell_small.IMG_HERO_QUALITY_0)
    self.img_hero_quality_0:setVisible(false)

    self.img_die_bg = findChildByTag(self,Tag_hero_cell_small.IMG_DIE_BG)
    self.lab_die_txt = findChildByTag(self,Tag_hero_cell_small.LAB_DIE_TXT)
    self.img_die_bg:setVisible(false)
    self.lab_die_txt:setVisible(false)

    self.img_star:setVisible(false)
    self.img_hero_level_bg:setVisible(false)
    self.lab_hero_level:setVisible(false)

--    self.prog_hero_soul_stone = findChildByTag(self,Tag_hero_cell_small.PROG_HERO_SOUL_STONE)
----    self.lab_hero_soul_stone_prog = findChildByTag(self,Tag_hero_cell_small.LAB_HERO_SOUL_STONE_PROG)
--    self.prog_hero_soul_stone:setMaxValue(100)
--    self.prog_hero_soul_stone:setValue(60)
--	self.prog_hero_soul_stone:setMinValue(0)
--    self:setHeroSoulStoneProgVisible(false)

    self.img_transparent_mask = findChildByTag(self,Tag_hero_cell_small.IMG_TRANSPARENT_MASK)
    self.img_transparent_mask:setVisible(false)

    self.img_up_1 = findChildByTag(self,Tag_hero_cell_small.IMG_UP_1)
    self.img_up_1:setVisible(false)

--    self.img_go_into_battle_tag = findChildByTag(self,Tag_hero_cell_small.IMG_GO_INTO_BATTLE_TAG)
--    self.img_go_into_battle_tag:setVisible(false)

    self.prog_hero_anger = findChildByTag(self,Tag_hero_cell_small.PROG_HERO_ANGER)
    self.prog_hero_anger:setVisible(false)

    self.prog_hero_hp = findChildByTag(self,Tag_hero_cell_small.PROG_HERO_HP)
    self.prog_hero_hp:setVisible(false)
    --self.img_star_bg = findChildByTag(self,Tag_hero_cell_small.IMG_STAR_BG)
    --self.img_star_bg:setVisible(false)
    -- 点击事件
    local function onClickHeroItem(pSender)
		if nil ~= self.onClickHeroItem then
			self.onClickHeroItem(p_sender,self.nHeroID)
		end
    end  
    self.btn_place:setOnClickScriptHandler(onClickHeroItem)
    self.btn_place:setSyncScaleParent(false)
end

-- 设置点击事件回调
function HeroCellSmall:setClickCallback(onClickFunc)
    self.onClickHeroItem = onClickFunc
end

-- 设置是否可点击
function HeroCellSmall:setClickEnabled(bEnable)
    self.btn_place:setEnabled(bEnable)
end

-- 设置头像框是否具有缩放效果
function HeroCellSmall:setClickScaleEnable(bEnable)
    self.btn_place:setSyncScaleParent(bEnable)
end

-- 设置英雄头像框
function HeroCellSmall:setHeroPortrait(nHeroID,nFlag)
    self.nHeroID = nHeroID
    self.img_hero_portrait:setSpriteFrame(self:createHeroPortrait80X80(nHeroID))
    if nFlag then
        self.img_hero_portrait:setFlipX(nFlag)
    end
end

-- 获取英雄头像对象 当需要设置成非英雄头像时可以使用该对象 设置成想要的图像
function HeroCellSmall:getHeroPortrait()
    return self.img_hero_portrait
end

-- 设置英雄品质框
function HeroCellSmall:setHeroQuality(nQualityID)

    if self.nHeroID == -1 then
        return
    end
    if nQualityID >= 12 then
        nQualityID = 11
    end
    --取base表配置
    if nQualityID == 0 then
        nQualityID = HeroData.dic_hero_base[self.nHeroID].initial_quality
    end
    
    local sPath = string.format("res/public/frame/img_quality_%d.png",nQualityID)
    local objQualityFrame = getFrame(sPath)
    if nil == objQualityFrame then
    	log.error("HeroCellSmall:setHeroQuality","no resource: "..sPath)
        return 
	  end

    self.img_quality:setSpriteFrame(objQualityFrame)
end

-- 设置英雄等级背景
function HeroCellSmall:setHeroLevelBg(nQualityID)
--    local nNewQualityId = nQualityID
--    if nQualityID > 4 and nQualityID < 8 then
--        nNewQualityId = 4
--    elseif nQualityID >= 8 and nQualityID <= 12 then
--        nNewQualityId = 5
--    else
--        nNewQualityId = nQualityID
--    end
--    local sPath = string.format("res/public/icon/img_small_hero_level_bg_%d.png",nNewQualityId)
--    local objHeroLevelBgFrame = getFrame(sPath)
--    if nil == objHeroLevelBgFrame then
--    	log.error("HeroCellSmall:setHeroLevelBg","no resource: "..sPath)
--        return 
--	end

--    self.img_hero_level_bg:setVisible(true)
--    self.img_hero_level_bg:setSpriteFrame(objHeroLevelBgFrame)
end

-- 设置英雄等级
function HeroCellSmall:setHeroLevel(nLevel)
    if nLevel == "" or nLevel == nil then
        self.lab_hero_level:setString("")
        return
    end
    local sLevel = string.format("%d",nLevel)
    log.debug("HeroCellSmall:setHeroLevel","sLevel: "..sLevel)
    self.lab_hero_level:setVisible(true)
    self.lab_hero_level:setString(sLevel)
    self.img_hero_level_bg:setVisible(true)
end

-- 设置英雄星级
function HeroCellSmall:setHeroStar(nStar)
    if nStar == nil then 
        self.img_star:setVisible(false)
        return
    end
    if nStar > 5 then
      self.img_star:setVisible(false)
      return
    end
    if self.nHeroID == -1 then
      return
    end
    if nStar == 0 and HeroData.dic_hero_base and HeroData.dic_hero_base[self.nHeroID] then
        --todo 不应该在这里用数据！！！ 
        nStar = HeroData.dic_hero_base[self.nHeroID].initial_star
    end
    if nStar~=0 then
        local sPath = string.format("res/public/icon/img_small_yellow_star_%d.png",nStar)
        local objStarFrame = getFrame(sPath)
        self.img_star:setVisible(true)
        --self.img_star_bg:setVisible(true)
        self.img_star:setSpriteFrame(objStarFrame)
    end
end

-- 创建80*80的英雄头像 此时从道具表取资源
function HeroCellSmall:createHeroPortrait80X80(nHeroID)
	local sPath = string.format("res/portrait/%d.png",nHeroID)
    local objHeroFrame = getFrame(sPath)
    if nil == objHeroFrame then
    	--log.error("HeroCellSmall:createHeroPortrait80X80","no resource: "..sPath)
    	sPath = string.format("res/portrait/%d.png",0)
	end

    objHeroFrame = getFrame(sPath)
    return objHeroFrame
end

--设置英雄灵魂石进度条
function HeroCellSmall:setHeroSoulStoneProg(nCount,nMaxCount)
    self.prog_hero_soul_stone:setMaxValue(100)
    self.prog_hero_soul_stone:setValue(nCount / nMaxCount * 100)
	self.prog_hero_soul_stone:setMinValue(0)

    self.lab_hero_soul_stone_prog:setString(string.format("%d/%d",nCount,nMaxCount))
    self:setHeroSoulStoneProgVisible(true)
end

--设置英雄灵魂石进度条是否可见
function HeroCellSmall:setHeroSoulStoneProgVisible(bVisible)
    self.prog_hero_soul_stone:setVisible(bVisible)
    self.lab_hero_soul_stone_prog:setVisible(bVisible)
end

--设置英雄等级控件是否可见
function HeroCellSmall:setHeroLevelVisible(bVisible)
    self.img_hero_level_bg:setVisible(bVisible)
    self.lab_hero_level:setVisible(bVisible)
end

--设置英雄星级控件是否可见
function HeroCellSmall:setHeroStarVisible(bVisible)
    self.img_star:setVisible(bVisible)
end

--设置英雄图标表面的黑色遮罩是否显示
function HeroCellSmall:setTransparentMaskVisible(bVisible)
    self.img_transparent_mask:setVisible(bVisible)
end

--设置英雄培养提示
function HeroCellSmall:setHeroImgUpTip(nImgId)
    if nImgId ~= 0 and nImgId ~= nil then
        self.img_up_1:setVisible(true)
        local sImgPath = string.format("res/public/icon/img_up_%d.png",nImgId)
        self.img_up_1:setSpriteFrame(sImgPath)
        self:runUpAndDown()
    else
        self.img_up_1:setVisible(false)
    end
end

-- 执行提示箭头上下晃动动作
function HeroCellSmall:runUpAndDown()
    local x,y = self.img_up_1:getPosition()
	local ccMoveToUp = cc.MoveTo:create(UP_DOWN_SPEED,cc.p(x,y + UP_DOWN_RANGE))
	local ccMoveToDown = cc.MoveTo:create(UP_DOWN_SPEED,cc.p(x,y - UP_DOWN_RANGE))
	local ccSequenceUpDown = cc.Sequence:create(ccMoveToUp,ccMoveToDown)
	local ccRepeatForever = cc.RepeatForever:create(ccSequenceUpDown)		 
	self.img_up_1:setPosition(cc.p(x,y))
	self.img_up_1:runAction(ccRepeatForever)
end

-- 获取站位按钮的对象
function HeroCellSmall:getBtnPlace()
    return self.btn_place
end

---- 设置上阵标志图片
--function HeroCellSmall:setImgGoIntoBattle(nFlag)
--    self.img_go_into_battle_tag:setVisible(nFlag)
--end

-- 设置血条数值
function HeroCellSmall:setProgHeroHp(nHp,nTotalHp)
    self.prog_hero_hp:setVisible(true)
    --初始化进度条
    self.prog_hero_hp:setMaxValue(nTotalHp)
    self.prog_hero_hp:setValue(nHp)
	self.prog_hero_hp:setMinValue(0)
end

-- 设置怒气条数值
function HeroCellSmall:setProgHeroAnger(nAnger,nTotalAnger)
    self.prog_hero_anger:setVisible(true)
    --初始化进度条
    self.prog_hero_anger:setMaxValue(nTotalAnger)
    self.prog_hero_anger:setValue(nAnger)
	self.prog_hero_anger:setMinValue(0)
end    

-- 设置怪物头像框
function HeroCellSmall:setBossPortrait(nBossID)
    local dicBossInfo = clone(BattleData.boss_tpl[nBossID])
    local nRoleID = dicBossInfo.boss_role

    -- 怪物的模型ID 可能是英雄模型 也可能是怪物模型 优先取英雄的模型
    local sPath = string.format("res/portrait/%d.png",nRoleID)
    local objHeroFrame = getFrame(sPath)
    -- 无法取到英雄模型 再去去怪物模型
    if nil == objHeroFrame then
	    sPath = string.format("res/portrait/boss_%d.png",nRoleID)
        objHeroFrame = getFrame(sPath)
        if nil == objHeroFrame then
    	    log.error("HeroCellSmall:setBossPortrait","no resource: "..sPath)
    	    sPath = string.format("res/portrait/boss_%d.png",1)
	    end
    end

    objHeroFrame = getFrame(sPath)
    self.img_hero_portrait:setSpriteFrame(objHeroFrame)
end

-- 将英雄头像置灰
function HeroCellSmall:setIconShader(bShader)
    if bShader then
        self.img_hero_portrait = Shader.apply(self.img_hero_portrait,Shader.SHADER_STONE)
    else
        self.img_hero_portrait = Shader.apply(self.img_hero_portrait,Shader.SHADER_NORMAL)
    end
end

-- 设置英雄死亡文字与图片
function HeroCellSmall:setHeroDie(nFlag)
    self.img_die_bg:setVisible(nFlag)
    self.lab_die_txt:setVisible(nFlag)
end

-- 设置背景图片
function HeroCellSmall:setHeroCellSmallBg(nFlag)
    self.img_hero_quality_0:setVisible(nFlag)
end